package com.github.edu.web.roleUser.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.admin.edu.oauth.server.api.entity.TSysOrganization;
import com.github.admin.edu.oauth.server.api.entity.TSysRole;
import com.github.admin.edu.oauth.server.api.entity.TSysRoleUser;
import com.github.admin.edu.oauth.server.api.entity.TSysUser;
import com.github.edu.client.common.service.impl.BaseAbstractService;
import com.github.edu.web.organization.domain.OrganizationManagerDomain;
import com.github.edu.web.role.domain.RoleManagerDomain;
import com.github.edu.web.roleUser.domain.RoleUserManagerDomain;
import com.github.edu.web.roleUser.service.RoleUserManagerService;
import com.github.edu.web.user.domain.UserManagerDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/8/12
 * Time: 19:35
 */
@Slf4j
@Service
public class RoleUserManagerServiceImpl extends BaseAbstractService implements RoleUserManagerService {

    @Autowired
    private RoleUserManagerDomain domain;

    @Override
    public JsonArray<TSysRoleUser> getAllByPage(TSysRoleUser tSysRoleUser, Pager pager) {
        String code = jsonEntity(tSysRoleUser, pager);
        JsonArray<TSysRoleUser> jsonArray = null;
        if (!StringUtils.isEmpty(code)) {
            String data = domain.getALLByPager(code);
            if (!StringUtils.isEmpty(data)) {
                jsonArray = JsonUtils.toCollection(data, new TypeReference<JsonArray<TSysRoleUser>>() {
                });
            }
        }
        return jsonArray;
    }

    @Override
    public TSysRoleUser getEntity(Long integer) {
        JsonEntity<TSysRoleUser> jsonEntity = JsonUtils.toCollection(domain.getEntity(integer), new TypeReference<JsonEntity<TSysRoleUser>>() {
        });
        return jsonEntity.getData();
    }

    @Override
    public TSysRoleUser saveOrUpdate(TSysRoleUser tSysRoleUser) {
        JsonEntity<TSysRoleUser> jsonEntity = new JsonEntity<>();
        jsonEntity.setData(tSysRoleUser);
        String code = domain.saveOrUpdate(JsonUtils.toJson(jsonEntity));
        JsonEntity<TSysRoleUser> json = JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSysRoleUser>>() {
        });
        if (null != json) {
            return json.getData();
        }
        return null;
    }

    @Override
    public String delete(String ids) {
        return domain.deleteAll(ids);
    }

    @Autowired
    private UserManagerDomain userManagerDomain;

    @Override
    public List<TSysUser> getAllUser() {
        String code=userManagerDomain.getAllEntity("","","");
        if(!StringUtils.isEmpty(code)){
            JsonArray<TSysUser> jsonArray=JsonUtils.toCollection(code, new TypeReference<JsonArray<TSysUser>>() {
            });
            if(null!=jsonArray){
                return jsonArray.getDataList();
            }
        }
        return null;
    }

    @Autowired
    private RoleManagerDomain roleManagerDomain;
    @Override
    public List<TSysRole> getAllRole() {
        String code=roleManagerDomain.getAllEntity("","","");
        if(!StringUtils.isEmpty(code)){
            JsonArray<TSysRole> jsonArray=JsonUtils.toCollection(code, new TypeReference<JsonArray<TSysRole>>() {
            });
            if(null!=jsonArray){
                return jsonArray.getDataList();
            }
        }
        return null;
    }

    @Autowired
    private OrganizationManagerDomain organizationManagerDomain;

    @Override
    public List<TSysOrganization> getAllOrganization() {
        String code=organizationManagerDomain.getAllEntity("","","");
        if(!StringUtils.isEmpty(code)){
            JsonArray<TSysOrganization> jsonArray=JsonUtils.toCollection(code, new TypeReference<JsonArray<TSysOrganization>>() {
            });
            if(null!=jsonArray){
                return jsonArray.getDataList();
            }
        }
        return null;
    }
}
